import { login, getInfo } from '@/api/login'
import {
  getToken,
  setToken,
  removeToken,
  getId,
  setId,
  removeId,
  getName,
  setName,
  removeName,
  getEmail,
  setEmail,
  removeEmail,
  getPhone,
  setPhone,
  removePhone
} from '@/utils/auth'

const user = {
  state: {
    token: getToken(),
    account: getName(),
    admin_id: getId(),
    email: getEmail(),
    phone: getPhone()
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_ID: (state, id) => {
      state.admin_id = id
    },
    SET_NAME: (state, account) => {
      state.account = account
    },
    SET_EMAIL: (state, email) => {
      state.email = email
    },
    SET_PHONE: (state, phone) => {
      state.phone = phone
    }

  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      return new Promise((resolve, reject) => {
        // login(userInfo.account, userInfo.password).then(response => {
        //   var data = response.data
          setToken('loginSuccess')
          // setId(data.id)
          commit('SET_TOKEN', 'loginSuccess')
          // commit('SET_ID', data.id)
          resolve()
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },
    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        // getInfo(state.admin_id, state.token).then(response => {
        //   const data = response.data
          setName('admin')
          // setEmail(data.email)
          // setPhone(data.phone)
          commit('SET_NAME', 'admin')
          // commit('SET_EMAIL', data.email)
          // commit('SET_PHONE', data.phone)
          resolve()
        // })
        // .catch(error => {
        //   reject(error)
        // })
      })
    },
    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        // removeId()
        // removeName()
        // removeEmail()
        // removePhone()
        resolve()
      })
    }
  }
}

export default user
